﻿namespace ChessToys
{
    using System;

    /// <summary>
    /// A position at a <see cref="Board"/>.
    /// </summary>
    public class Position : AbstractPair<int, int>
    {
        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The a.</param>
        /// <param name="b">The b.</param>
        /// <returns>The result of the operator.</returns>
        public static Position operator +(Position a, Position b)
        {
            return new Position(a.X + b.X, a.Y + b.Y);
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The a.</param>
        /// <param name="b">The b.</param>
        /// <returns>The result of the operator.</returns>
        public static Position operator -(Position a, Position b)
        {
            return new Position(a.X - b.X, a.Y - b.Y);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Position"/> class.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        public Position(int x, int y)
            :base (x, y)
        {
        }

        /// <summary>
        /// Gets the X.
        /// </summary>
        /// <value>The X.</value>
        public int X
        {
            get { return First; }
        }

        /// <summary>
        /// Gets the Y.
        /// </summary>
        /// <value>The Y.</value>
        public int Y
        {
            get { return Second; }
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }
            if (obj.GetType() != typeof(Position))
            {
                return false;
            }
            Position other = obj as Position;
            return X == other.X && Y == other.Y;
        }

        public override int GetHashCode()
        {
            return 31 * X.GetHashCode() + 51 * Y.GetHashCode();
        }

        /// <inheritdoc />
        public override String ToString()
        {
            return String.Format("[{0}, {1}]", X, Y);
        }

        public String ToString(Board board)
        {
            return String.Format("{0}{1}", (char)('a' + X), board.Size - Y);
        }
    }
}
